package com.maserb.monitor.service.schoolStudent.impl;

import com.maserb.monitor.common.CreateTimeUtils;
import com.maserb.monitor.common.IdUtils;
import com.maserb.monitor.common.JpushImClientUtil;
import com.maserb.monitor.entity.schoolStudent.SchoolStudentEntity;
import com.maserb.monitor.entity.schoolStudent.SchoolStudentParentEntity;
import com.maserb.monitor.repository.schoolStudent.SchoolStudentParentRepository;
import com.maserb.monitor.service.base.impl.JpaSpecificationServiceImpl;
import com.maserb.monitor.service.schoolStudent.SchoolStudentParentService;
import com.maserb.monitor.service.schoolStudent.SchoolStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.StringUtils;

import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.List;

@Service
public class SchoolStudentParentServiceImpl extends JpaSpecificationServiceImpl<SchoolStudentParentRepository,
        SchoolStudentParentRepository, SchoolStudentParentEntity, String>
        implements SchoolStudentParentService {

    @Autowired
    private SchoolStudentService studentService;

    private Specification<SchoolStudentParentEntity> getSpecification(String userName, String realName, String phoneNumber, String idcard) {
        return new Specification<SchoolStudentParentEntity>()  {
            @Override
            public Predicate toPredicate(Root<SchoolStudentParentEntity> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                Path<String> pathRealName = root.get("realName");
                Path<String> pathUserName = root.get("userName");
                Path<String> pathPhoneNumber = root.get("phoneNumber");
                Path<String> pathIdcard = root.get("idcard");
                Path<String> pathIsDel = root.get("isDel");
                ArrayList<Predicate> list = new ArrayList<Predicate>();
                if (!StringUtils.isEmptyOrWhitespace(realName)) {
                    list.add(criteriaBuilder.and(criteriaBuilder.like(pathRealName, "%" + realName + "%")));
                }
                if (!StringUtils.isEmptyOrWhitespace(userName)) {
                    list.add(criteriaBuilder.and(criteriaBuilder.like(pathUserName, "%" + userName + "%")));
                }
                if (!StringUtils.isEmptyOrWhitespace(phoneNumber)) {
                    list.add(criteriaBuilder.and(criteriaBuilder.like(pathPhoneNumber, "%" + phoneNumber + "%")));
                }
                if (!StringUtils.isEmptyOrWhitespace(idcard)) {
                    list.add(criteriaBuilder.and(criteriaBuilder.like(pathIdcard, "%" + idcard + "%")));
                }
                list.add(criteriaBuilder.and(criteriaBuilder.equal(pathIsDel, "0")));
                return criteriaBuilder.and(list.toArray(new Predicate[list.size()]));
            }
        };
    }



    @Override
    public Page<SchoolStudentParentEntity> findAll(Pageable pageable, String userName, String realName, String phoneNumber, String idcard) {
        return this.findAll(getSpecification(userName,realName,phoneNumber,idcard), pageable);
    }

    @Override
    public List<SchoolStudentParentEntity> findAll( String userName, String realName, String phoneNumber, String idcard) {
        return new ArrayList( this.findAll(getSpecification(userName,realName,phoneNumber,idcard),new PageRequest(0,1000)).getContent());
    }

    @Override
    public SchoolStudentParentEntity findByUserName(String userName) {
        return this.repository.findByUserName(userName);
    }

    @Override
    public void doReset(String id) {
        SchoolStudentParentEntity entity = this.findOne(id);
        entity.setPassword("123456");
        this.save(entity);
    }

    @Override
    public void saveByStudentId(List<SchoolStudentParentEntity> list) {
        for(SchoolStudentParentEntity parentEntity:list){
            SchoolStudentParentEntity parentEntity2 = this.findByUserName(parentEntity.getUserName());
            if(null!=parentEntity2 && null!=parentEntity2.getId()&&!"".equals(parentEntity2.getId())){
                continue;
            }
            parentEntity.setCreateTime(CreateTimeUtils.getCreateTime());
            parentEntity.setPassword("123456");
            parentEntity.setSalt(IdUtils.generateId());
            parentEntity.setIsDel("0");
            this.save(parentEntity);
            if(!StringUtils.isEmptyOrWhitespace(parentEntity.getStudentIds())){
                SchoolStudentEntity student = studentService.findByStudentNo(parentEntity.getStudentIds());
                if(null!=student && !StringUtils.isEmptyOrWhitespace(student.getId())){
                    student.setParent(parentEntity);
                    studentService.save(student);
                }
            }
            JpushImClientUtil.addUser(parentEntity.getUserName(),parentEntity.getPassword(),parentEntity.getRealName());
        }
    }

    @Override
    public List<SchoolStudentParentEntity> findAll(String clazzId) {
        return repository.findByClazzId(clazzId);
    }

    @Override
    public List<Object[]> findObjectByClazzId(String clazzId) {
        return repository.findObjectByClazzId(clazzId);
    }


}